package mango.web;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import mango.functions.FuncString;

public class FormChain {
    private HashMap<String, ArrayList<String>> requestParam = new HashMap<String, ArrayList<String>>();
    private HashMap<String, ArrayList<InputStream>> requestUploadStreams = new HashMap<String, ArrayList<InputStream>>();
    private HashMap<String, Object> extraParam = new HashMap<String, Object>();
    private HttpServletRequest request;

    public FormChain(HttpServletRequest request) {
        this.request = request;
    }

    public FormChain putExtra(String key, Object value) {
        this.extraParam.put(key, FuncString.encodeIfString(value));
        return this;
    }

    public Object getExtra(String key) {
        return this.extraParam.get(key);
    }

    public void setRequestParameter(String key, String value) {
        this.setRequestParameters(key.toUpperCase(),
                new String[] { FuncString.encodeString(value) });
    }

    public void setRequestParameters(String key, String[] values) {
        ArrayList<String> val = this.requestParam.get(key.toUpperCase());
        if (val == null) {
            val = new ArrayList<String>();
            this.requestParam.put(key.toUpperCase(), val);
        }
        for (String value : values) {
            val.add(FuncString.encodeString(value));
        }
    }

    public void setRequestUploadStream(String key, InputStream stream) {
        ArrayList<InputStream> val = this.requestUploadStreams.get(key.toUpperCase());
        if (val == null) {
            val = new ArrayList<InputStream>();
            this.requestUploadStreams.put(key.toUpperCase(), val);
        }
        val.add(stream);
    }

    public ArrayList<String> getRequestParameters(String key) {
        ArrayList<String> ret = this.requestParam.get(key.toUpperCase());
        if (ret == null) {
            ret = new ArrayList<String>();
        }
        return ret;
    }

    public String getRequestParameter(String key) {
        ArrayList<String> list = this.getRequestParameters(key.toUpperCase());
        if (list.size() != 0) {
            return list.get(0);
        }
        return null;
    }

    public ArrayList<InputStream> getRequestUploadStreams(String key) {
        ArrayList<InputStream> ret = this.requestUploadStreams.get(key.toUpperCase());
        if (ret == null) {
            ret = new ArrayList<InputStream>();
        }
        return ret;
    }

    public InputStream getRequestUploadStream(String key) {
        ArrayList<InputStream> list = this.getRequestUploadStreams(key);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public Object getSessionAttr(String key) {
        return this.request.getSession().getAttribute(key);
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Iterator<String> iter = this.requestParam.keySet().iterator(); iter.hasNext();) {
            String k = iter.next();
            sb.append(k)
                    .append(":")
                    .append(requestParam.get(k).size() > 1 ? requestParam.get(k) : requestParam
                            .get(k).get(0)).append("|");
        }
        return sb.toString();
    }
}
